//
//  GoodsNumSelectView.m
//  WeiYiMei
//
//  Created by iMac on 2019/4/18.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "GoodsNumSelectView.h"

@interface GoodsNumSelectView ()

@property (nonatomic, strong) UIButton *addButton;
@property (nonatomic, strong) UIButton *deleteButton;
@property (nonatomic, strong) UILabel *numLab;

@end

@implementation GoodsNumSelectView

- (void)setupSubViews
{
    self.addButton = [[UIButton alloc] init];
    [self.addButton setTitle:@"+" forState:UIControlStateNormal];
    [self.addButton setTitleColor:kColor333333 forState:UIControlStateNormal];
    self.addButton.titleLabel.font = [UIFont systemFontOfSize:14];
    self.addButton.backgroundColor = kColorF2F2F2;
    self.addButton.layer.borderColor = kColorDDD.CGColor;
    self.addButton.layer.borderWidth = 1/kScale;
    [self addSubview:self.addButton];
    
    self.deleteButton = [[UIButton alloc] init];
    [self.deleteButton setTitle:@"-" forState:UIControlStateNormal];
    [self.deleteButton setTitleColor:kColor333333 forState:UIControlStateNormal];
    self.deleteButton.titleLabel.font = [UIFont systemFontOfSize:14];
    self.deleteButton.backgroundColor = kColorF2F2F2;
    self.deleteButton.layer.borderColor = kColorDDD.CGColor;
    self.deleteButton.layer.borderWidth = 1/kScale;
    [self addSubview:self.deleteButton];
    
    self.numLab = [[UILabel alloc] init];
    self.numLab.textColor = kColor333333;
    self.numLab.textAlignment = NSTextAlignmentCenter;
    self.numLab.font = [UIFont systemFontOfSize:14];
    self.numLab.text = @"1";
    self.numLab.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.numLab];
    
    [self.addButton addTarget:self action:@selector(addAcion:) forControlEvents:UIControlEventTouchUpInside];
    [self.deleteButton addTarget:self action:@selector(deleteAcion:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setupConstraints
{
    [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self);
        make.width.equalTo(self.deleteButton.mas_height).multipliedBy(1);
    }];
    
    [self.numLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.deleteButton.mas_right);
        make.top.bottom.equalTo(self);
        
    }];
    
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.equalTo(self);
        make.left.equalTo(self.numLab.mas_right);
        make.width.equalTo(self.deleteButton);
    }];
}

- (void)addAcion:(id)sender
{
    int num = self.numLab.text.intValue;
    if (num < 100) {
        num++;
        self.numLab.text = [NSString stringWithFormat:@"%d",num];
    }
    if (self.addOrDeleteHandler) {
        self.addOrDeleteHandler(num);
    }
}

- (void)deleteAcion:(id)sender
{
    int num = self.numLab.text.intValue;
    if (num > 0) {
        num--;
        self.numLab.text = [NSString stringWithFormat:@"%d",num];
    }
    if (self.addOrDeleteHandler) {
        self.addOrDeleteHandler(num);
    }
}

- (int)num
{
    return self.numLab.text.intValue;
}

- (void)setNumber:(NSString *)number
{
    self.numLab.text = number;
}

@end
